package org.mspring.mlog.web.module.web.api;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
import org.mspring.mlog.api.duoshuo.DSConst;
import org.mspring.mlog.api.duoshuo.HmacSHA1;
import org.mspring.platform.utils.JSONUtils;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 多说API所用到的Widget
 * 
 * @author Gao Youbo
 * @since 2013年9月24日
 */
@Widget
@RequestMapping("/api/duoshuo")
public class API_DuoshuoWidget {
    // http://localhost:8080/api/duoshuo/callback?action=sync_log&signature=Emx1uoISLHKqoRNie2Qi/ukJUk4\u003d
    @RequestMapping("/callback")
    public void callback(HttpServletRequest request, HttpServletResponse response) throws IOException, InvalidKeyException, NoSuchAlgorithmException {
        Map<String, String[]> map = request.getParameterMap();
        String JSON = IOUtils.toString(request.getInputStream(), "UTF-8");
        String JSON_MAP = JSONUtils.toJson(map);
        System.out.println(JSON_MAP + "\n==================================================================================================\n" + JSON);

        // {"action":["sync_log"],"signature":["Emx1uoISLHKqoRNie2Qi/ukJUk4\u003d"]}

        String signature = request.getParameter("signature");
        String baseString = request.getQueryString();

        // System.out.println(baseString);

        String expectSignature = HmacSHA1.hmac_sha1(baseString, DSConst.SECRET);
        System.out.println(signature);
        System.out.println(expectSignature);

        // String expectSignature = getSignature(baseString.getBytes("UTF-8"),
        // DSConst.SECRET.getBytes("UTF-8"));
        // System.out.println(signature);
        // System.out.println(expectSignature);
        //
        // SecretKey secretKey = new
        // SecretKeySpec(DSConst.SECRET.getBytes("UTF-8"), HMAC_SHA1);
        // Mac mac = Mac.getInstance(HMAC_SHA1);
        // mac.init(secretKey);
        // String a = new String(mac.doFinal(baseString.getBytes("UTF-8")),
        // "UTF-8");
        // System.out.println(a);
    }

    private static final String HMAC_SHA1 = "HmacSHA1";

    /**
     * 生成签名数据
     * 
     * @param data
     *            待加密的数据
     * @param key
     *            加密使用的key
     * @return 生成MD5编码的字符串
     * @throws InvalidKeyException
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     */
    public static String getSignature(byte[] data, byte[] key) throws InvalidKeyException, NoSuchAlgorithmException, UnsupportedEncodingException {
        SecretKeySpec signingKey = new SecretKeySpec(key, HMAC_SHA1);
        Mac mac = Mac.getInstance(HMAC_SHA1);
        mac.init(signingKey);
        byte[] rawHmac = mac.doFinal(data);
        return new String(DigestUtils.md5(rawHmac), "UTF-8");
    }

}